package com.tencent.wuyanzu.whattoeat;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;

import com.tencent.wuyanzu.whattoeat.Entity.Food;

/**
 * Created by maiffany on 16/7/24.
 */
public class MyPopUpWindows extends PopupWindow {
    private View mainView;
    public MyPopUpWindows(final Activity context, View.OnClickListener itemclick){
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        mainView = inflater.inflate(R.layout.pop_window,null);
        this.setContentView(mainView);
        this.setFocusable(true);
        this.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        Button confirmButton = (Button) mainView.findViewById(R.id.confirm_btn);
        Intent intent=context.getIntent();
        final Food selectFood=(Food)intent.getExtras().get("foodDetail");

        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context,MealTimeActivity.class);
                SharedPreferences sharedPreferences=context.getSharedPreferences("whattoeat", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putString("name",selectFood.getName());
                editor.putString("url",selectFood.getUrl());
                editor.putInt("up",selectFood.getUp());
                editor.putInt("down",selectFood.getDown());
                editor.putString("phoneNumber",selectFood.getPhoneNumber());
                editor.putString("address",selectFood.getAddress());
                editor.putString("restaurant",selectFood.getRestaurant());
                intent.putExtra("foodDetail",selectFood);
                editor.commit();
                context.startActivity(intent);
                context.finish();

            }
        });
        Button cancelButton = (Button) mainView.findViewById(R.id.cancel_button);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SelectActivity.class);
                context.startActivity(intent);
                context.finish();
            }
        });
    }
}
